/*
 * @Author: 江舟 jijiangzhou@recycloud.cn
 * @Date: 2024-03-25 15:27:04
 * @LastEditors: 江舟 jijiangzhou@recycloud.cn
 * @LastEditTime: 2024-06-06 15:10:38
 * @FilePath: \vite-project\src\hooks\useTableAction.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { type ActionType } from '@ant-design/pro-components';
import { Modal, message } from 'antd';
import { useRef, useState } from 'react';

export default function useTableAction(name = '') {
	const [messageApi] = message.useMessage();
	const tableRef = useRef<ActionType>();

	const [visible, setVisible] = useState(false);
	const [editFormData, setEditFormData] = useState<any>({});
	const [formLoading, setFormLoading] = useState(false);
	const [title, setTitle] = useState('');
	const [formDisabled, setFormDisabled] = useState(false);

	const refresh = () => {
		tableRef.current?.reload();
	};

	const commonAction: ({
		title,
		content,
		api,
	}: {
		title: string;
		content: string;
		api: () => Promise<unknown>;
		successMsg: string;
		successCallback?: () => void;
	}) => void = ({ title, content, api, successMsg, successCallback = () => {} }) => {
		Modal.confirm({
			title,
			content,
			okText: '确定',
			cancelText: '取消',
			centered: true,
			onOk() {
				return new Promise<void>(async (resolve, reject) => {
					try {
						await api();
						messageApi.success(successMsg);
						resolve();
						refresh();
						successCallback?.();
					} catch (error) {
						reject();
					}
				});
			},
		});
	};

	const handleAdd = (callback?: () => void) => {
		setTitle(`创建` + name);
		setVisible(true);
		callback?.();
	};

	const handleClose = () => {
		setTitle('');
		setFormDisabled(false);
		setVisible(false);
		setEditFormData({});
	};

	const handleEdit = async (redord: object, formatRecord?: (data?: any) => any | null, detailApi?: () => void) => {
		setTitle(`编辑` + name);
		setVisible(true);
		let data: any = redord;
		if (detailApi) {
			setFormLoading(true);
			try {
				data = await detailApi?.();
			} finally {
				if (formatRecord) {
					data = formatRecord?.(data);
				}
				setEditFormData({
					...data,
				});
				setFormLoading(false);
			}
		} else {
			if (formatRecord) {
				data = formatRecord?.(data);
			}
			setEditFormData({
				...data,
			});
		}
	};

	const handleView = async (redord: object, formatRecord?: (data?: any) => any | null, detailApi?: () => void) => {
		setTitle(`查看` + name);
		setFormDisabled(true);
		setVisible(true);
		let data: any = redord;
		if (detailApi) {
			setFormLoading(true);
			try {
				data = await detailApi?.();
			} finally {
				if (formatRecord) {
					data = formatRecord?.(data);
				}
				setEditFormData({
					...data,
				});
				setFormLoading(false);
			}
		} else {
			if (formatRecord) {
				data = formatRecord?.(data);
			}
			setEditFormData({
				...data,
			});
		}
	};

	const handleDel = (api: () => Promise<unknown>, { content = '确定要删除吗', successMsg = '删除成功', successCallback = () => {} }) => {
		commonAction({
			title: '提示',
			content,
			api,
			successMsg,
			successCallback,
		});
	};

	const handleDisEnableStatus: (
		api: () => Promise<unknown>,
		{
			successMsg,
			status,
		}: {
			successMsg?: string | undefined;
			status?: 0 | 1;
		},
	) => void = (api, { successMsg = '删除成功', status = 1 }) => {
		commonAction({
			title: '提示',
			content: `确定要${status === 1 ? '禁用' : '启用'}吗`,
			api,
			successMsg,
		});
	};

	return {
		formLoading,
		editFormData,
		setEditFormData,
		visible,
		tableRef,
		refresh,
		commonAction,
		handleAdd,
		handleClose,
		handleView,
		handleEdit,
		handleDel,
		handleDisEnableStatus,
		title,
		formDisabled,
	};
}
